package com.qfmy.web.app.service.impl.conmon;

import com.qfmy.common.exception.BaseException;
import com.qfmy.common.login.LoginUser;
import com.qfmy.common.login.LoginUserHolder;
import com.qfmy.common.result.ResultCodeEnum;
import com.qfmy.model.entity.common.UserSignRecord;
import com.qfmy.model.vo.common.UserSignRecordVo;
import com.qfmy.web.app.mapper.common.UserSignRecordMapper;
import com.qfmy.web.app.mapper.user.UserMapper;
import com.qfmy.web.app.service.conmon.UserSignRecordService;
import org.jetbrains.annotations.Contract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author 清风明月
 * @Package com.qfmy.web.app.service.impl.conmon
 * @date 2025/11/6
 * @description 用户签到表
 */
@Service
public class UserSignRecordServiceImpl implements UserSignRecordService {

    /**
     * 注入mapper
     */
    @Autowired
    private UserSignRecordMapper userSignRecordMapper;

    /**
     * 注入用户mapper
     */
    @Autowired
    private UserMapper userMapper;


    /**
     * 签到
     *
     * @param reward
     * @param day
     */
    @Override
    public void signIn(BigDecimal reward, Integer day) {
        //获取当前用户id
        Long userId = getUserId();
        //获取当前日期
        LocalDate now = LocalDate.now();
        //根据用户id和当前日期查询签到记录
        UserSignRecord userSignRecord = userSignRecordMapper.getUserSignRecordByUserIdAndSignDate(userId);
        //判断用户是否已签到
        if (userSignRecord != null) {
           //判断是否签到
            if (userSignRecord.getIsSigned() == 1) {
                throw new BaseException(ResultCodeEnum.USER_SIGN_IN_ALREADY);
            }else{
                userSignRecord.setIsSigned(1);
                userSignRecord.setSignReward(reward);
                userSignRecord.setContinuousDays(day);
                userSignRecordMapper.update(userSignRecord);
            }
        }else{
            //创建签到对象
            userSignRecord = new UserSignRecord();
            userSignRecord.setUserId(userId);
            userSignRecord.setSignDate(now);
            userSignRecord.setIsSigned(1);
            userSignRecord.setSignReward(reward);
            userSignRecord.setContinuousDays(day);
            userSignRecordMapper.insert(userSignRecord);
        }

        //更新用户余额
        userMapper.updateAccount(userId, reward);
    }

    /**
     * 获取用户签到信息
     * @return
     */
    @Override
    public UserSignRecordVo getUserSigIn() {
        //获取用户id
        Long userId = getUserId();
        //判断用户是否登录
        if (userId == null || userId <= 0) {
            return null;
        }
        //根据用户id查询签到记录
        UserSignRecord userSignRecord = userSignRecordMapper.getUserSignRecordByUserIdAndSignDate(userId);
        //判断是否为空
        if (userSignRecord != null) {
            return new UserSignRecordVo(userSignRecord.getId(), userSignRecord.getSignDate(), userSignRecord.getIsSigned(), userSignRecord.getSignReward(), userSignRecord.getContinuousDays());
        }
        return null;
    }

    /**
     * 获取当前用户id
     * @return
     */
    @Contract(pure = true)
    private Long getUserId() {
        //获取当前用户id
        LoginUser loginUser = LoginUserHolder.getLoginUser();
        //判断用户是否登录
        if (loginUser == null) {
            throw  new BaseException(ResultCodeEnum.TOKEN_INVALID);
        }
        return loginUser.getUserId();
    }
}
